//
//  VideoListController.m
//  OnionHome
//
//  Created by fengjie on 2019/10/8.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "VideoListController.h"

@interface VideoListController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;

@end

@implementation VideoListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.listView];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
-(void)reloadData{
    [self.listView reloadData];
}
#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return kAppDelegate.videoArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    VideoListItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(VideoListItem.class) forIndexPath:indexPath];
    cell.row = indexPath.row;
    cell.model =  kAppDelegate.videoArray[indexPath.row];
    [cell loadImage:indexPath];
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BlockSafeRun(self.didselBlock, kAppDelegate.videoArray[indexPath.row]);
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (KScreenWidth - 3)/4;
    return CGSizeMake(width, width);
}
// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}
// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
}

-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        _listView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _listView.backgroundColor=KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        _listView.scrollEnabled = YES;
        [_listView registerClass:[VideoListItem class] forCellWithReuseIdentifier:NSStringFromClass(VideoListItem.class)];
    }
    return _listView;
}
@end

@interface VideoListItem ()
/**<#属性#>*/
@property (nonatomic,strong)UIImageView*imageView;
/**<#属性#>*/
@property (nonatomic,strong)UILabel*timerLabel;

@end

@implementation VideoListItem

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.selectedBackgroundView.backgroundColor = UIColorFromRGB(0x009966);
        
        [self addSubview:self.imageView];
        [self addSubview:self.timerLabel];
        
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(0.5);
            make.right.bottom.mas_equalTo(-0.5);
        }];
        [self.timerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-5);
            make.bottom.mas_equalTo(-5);
        }];
    }
    return self;
}
-(void)setModel:(VideoPlayModel *)model{
    _model = model;
    if (!model.asset) {
        return;
    }
    if (model.asset.duration < 10) {
        self.timerLabel.text = [NSString stringWithFormat:@"0:0%.f",model.asset.duration];
    }else if (model.asset.duration >= 10 && model.asset.duration <60){
        self.timerLabel.text = [NSString stringWithFormat:@"0:%.f",model.asset.duration];
    }else{
//        self.timerLabel.text = [NSString stringWithFormat:@"%f:%.f",asset.duration/60,asset.duration%60];
    }
}

#pragma mark - 加载图片
-(void)loadImage:(NSIndexPath *)indexPath {
    if (!self.model.asset) {
        [self.imageView imageViewWithUrl:self.model.pictureUrl placeholderImage:@""];
        return;
    }
    CGFloat imageWidth = (KScreenWidth - 3.f) / 4;
    self.imageView.image = nil;
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    // 同步获得图片, 只会返回1张图片
    options.synchronous = NO;
    
    [[PHCachingImageManager defaultManager] requestImageForAsset:self.model.asset targetSize:CGSizeMake(imageWidth * [UIScreen mainScreen].scale, imageWidth * [UIScreen mainScreen].scale) contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        if (self.row == indexPath.row ) {
            self.imageView.image = result;
        }
    }];
}
-(UIImageView*)imageView{
    if(!_imageView){
        _imageView=[UIImageView new];
        _imageView.image = IMAGE_NAMED(@"dynamicDefut");
    }
    return _imageView;
}
-(UILabel*)timerLabel{
    if(!_timerLabel){
        _timerLabel=[UILabel new];
        _timerLabel.textColor = KWhiteColor;
        _timerLabel.font = SYSTEMFONT(11);
        _timerLabel.text = @"0:19";
    }
    return _timerLabel;
}
@end
